(function(S) {
	S.declare('Dialog', S.Widget, {
		node : null,
		title : 'SimpleDialog',
		className : 'simple-dialog',
		modal : true,
		unModal : true,
		position : '',
		ajax : '',
		loadingMessage : '加载中...',
		url : '',
		iframe : false,
		buttons : false,
		handleTool : ['close'],
		maxContainer : "body",
		width : "",
		height : "",
		data : "",
		onmin : $.noop,
		onmax : $.noop,
		onload : $.noop,
		onclose : $.noop,
		draggable : true,
		templateString : '<table class="simple-dialog-table">' + '<tr class="handle"><td class="simple-dialog-tl"></td><td class="simple-dialog-tc" rel="dialogHead"><h2 class="simple-dialog-title" rel="dialogTitle"></h2></td><td class="simple-dialog-tr"></td></tr>' + '<tr><td class="simple-dialog-cl"></td><td class="simple-dialog-cc" ><div class="simple-dialog-content" rel="dialogBody"></div><div rel="dialogFoot"></div></td><td class="simple-dialog-cr"></td></tr>' + '<tr><td class="simple-dialog-bl"></td><td class="simple-dialog-bc"></td><td class="simple-dialog-br"></td></tr></table>',
		init : function() {
			this.container.append(this.templateString);
			this.dialog = this.container;
			//this.node = this.dialog;
			this.head = this.container.find("[rel='dialogHead']");
			this.titleContainer = this.container.find("[rel='dialogTitle']");
			this.body = this.container.find("[rel='dialogBody']");
			this.foot = this.container.find("[rel='dialogFoot']");
			//this.constructor.stackDialog.dialog.push(this.dialog);
			//this.constructor.stackDialog.focus = this.dialog;
		},
		renderUI : function() {
			var self = this, bodyContent, maxIndex;
			if (this.modal) {
				//maxIndex = this.getMaxIndex();
				this.modal = this.constructor.stackDialog.modal = $(document.body).modal();
				//this.constructor.stackDialog.modal.data('widget-modal').container.css('z-index', maxIndex + 1);
			}
			if (this.node[0]) {
				bodyContent = this.node.clone(true);
			} else if (this.ajax) {
				this.container.addClass("simple-dialog-ajax");
				bodyContent = "<div class='simple-dialog-load'>" + this.loadingMessage + "</div>";
				this._getAjaxHtml(this.url, function(r) {
					self.body.html(r);
					self.setPosition(self.position);
					self.onload.call(self.dialog);
				});
			} else if (this.iframe) {
				this.dialog.addClass("simple-dialog-iframe");
				bodyContent = '<div class="simple-dialog-load">' + this.loadingMessage + '</div><iframe class="simple-content-iframe" src="' + this.url + '" frameBorder="0" name="SimpleDialogFrame" style="display:none"></iframe>';
			}
			this.body.html(bodyContent);
			this.titleContainer.html(this.title);
			this.setHandleTool(this.handleTool);
			this.setButtons(this.buttons);
			this.node = this.dialog;

			if (this.iframe) {
				window["SimpleDialogFrame"].onload = function() {
					self.body.find(".simple-dialog-load").remove();
					self.body.find("iframe").fadeIn();
					self.setPosition(self.position);
				}
			}
			this.body.width(this.width).height(this.height);
			this.setPosition(this.position);
			if (this.draggable) {
				this.dialog.drag({
					handle : "[rel='dialogHead']"
				});
			}

			this.dialog.css('z-index', (maxIndex + 2) || ' ');
		},
		bindUI : function() {
			var self = this;
			//设定关闭事件
			this.head.find("[rel='dialogclose']").click(function() {
				if (self.onclose.call(self.dialog) === false) {
					return false;
				}
				self.destroy();
			});
			//设定最大化事件
			this.head.click($.proxy(this.setFocus, this));
		},
		setTitle : function(title) {
			this.titleContainer.html(title);
		},
		setContent : function(content) {
			this.body.html(content);
			this.setPosition(this.position);
		},
		_getAjaxHtml : function(url, callback) {
			if (S.ajaxDialog.html[url]) {
				callback(S.ajaxDialog.html[url]);
			} else {
				$.ajax({
					url : this.url,
					dataType : "html",
					data : this.data,
					success : function(r) {
						callback(r);
					},
					error : function() {

					}
				});
			}
		},
		setHandleTool : function(tools) {
			//this.head.empty();
			S.each(tools, function(n, i) {
				var tool = $("<a/>").addClass("simple-dialog-" + n).attr("rel", "dialog" + n).html(n);
				tool.click(this["on" + n]);
				this.head.append(tool);
			}, this);
		},
		setSize : function() {
			var maxCon = $(this.maxContainer), offset = maxCon.offset(), width = maxCon.width();
			height = maxCon.height();
			if (maxCon.is("body")) {
				height = $(window).height();
			}
			this.dialog.css(offset);
			this.dialog.width(width).height(height);
		},
		setButtons : function(buttons) {
			if (buttons) {
				var btnContainer = $("<div>").addClass("simple-dialog-btn-wrap");
				this.foot.empty().append(btnContainer);
				this.buttonInc = new S.Button({
					container : btnContainer,
					items : buttons
				});
			}
		},
		getButtons : function() {
			return this.buttonInc;
		},
		hide : function() {
			this.inherit(arguments);
			if (this.modal) {
				this.modal.data('widget-modal').hide();
			}
		},
		setFocus : function() {
			var maxIndex = this.getMaxIndex();
			if (this.dialog.css('z-index') == maxIndex) {
				return false;
			}
			this.dialog.css('z-index', maxIndex + 2);
			this.constructor.stackDialog.focus = this.dialog;
		},
		getFocus : function() {
			return this.constructor.stackDialog.focus;
		},
		getMaxIndex : function() {
			var zIndexList = [], maxIndex;
			S.each(this.constructor.stackDialog.dialog, function(dialog) {
				zIndexList.push(dialog.css('z-index'));
			});
			maxIndex = Math.max.apply(this, zIndexList);
			return maxIndex;
		},
		destroy : function() {
			this.modal.data('widget-modal').hide();
			this.inherit(arguments);
		}
	});
	S.Dialog.stackDialog = {
		dialog : [],
		focus : null,
		modal : null
	}
	S.ajaxDialog = $.ajaxDialog = function(url, config) {
		config = $.extend({}, config, {
			url : url,
			ajax : true
		});
		return new S.Dialog(config);
	}
	S.ajaxDialog.html = {};

})(Simple);
